package com.agileai.portal.extend.forum;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.common.BeanFactory;
import com.agileai.hotweb.domain.core.User;
import com.agileai.portal.bizmoduler.forum.ForumConst;
import com.agileai.portal.bizmoduler.forum.UserLevel8BBSUserInfoManage;
import com.agileai.portal.privilege.PrivilegeInterceptor;
import com.agileai.util.DateUtil;
import com.agileai.util.StringUtil;

public class ForumPrivilegeInterceptor implements PrivilegeInterceptor{
	
	public static HashMap<String,Integer> UserLoginCountCache = new HashMap<String,Integer>();
	@Override
	public void initExtendPrivilege(User user) {
		HashMap<String,Object> userForumProperties = new HashMap<String,Object>();
		UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = lookupService(UserLevel8BBSUserInfoManage.class);
		String userCode = user.getUserCode();
		DataRow fuInfo = userLevel8BBSUserInfoManage.retrieveForumUserRecord(userCode,null);
		DataParam param = new DataParam("FU_ID",fuInfo.getString("FU_ID"));
		List<DataRow> fuIdentitys = userLevel8BBSUserInfoManage.queryUserIdentitys(param);
		HashMap<String,Object> userIdentitys = new HashMap<String,Object>();
		for(int i=0;i<fuIdentitys.size();i++){
			DataRow userIdentity = fuIdentitys.get(i);
			HashMap<String,String> userIdentitys1 = new HashMap<String,String>();
			userIdentitys1.put("ROLE_ID", userIdentity.getString("ROLE_ID"));
			userIdentitys1.put("ROLE_NAME", userIdentity.getString("ROLE_NAME"));
			userIdentitys.put(String.valueOf(i), userIdentitys1);
		}
		userForumProperties.put("userIdentitys",userIdentitys);
		userForumProperties.put("FU_ID",fuInfo.getString("FU_ID"));
		userForumProperties.put("FU_CODE",fuInfo.getString("FU_CODE"));
		userForumProperties.put("FU_NAME",fuInfo.getString("FU_NAME"));
		userForumProperties.put("FU_LEVEL",fuInfo.getString("FU_LEVEL"));
		userForumProperties.put("FU_STATE",fuInfo.getString("FU_STATE"));
		userForumProperties.put("FU_TEL",fuInfo.getString("FU_TEL"));
		userForumProperties.put("FU_EMAIL",fuInfo.getString("FU_EMAIL"));
		userForumProperties.put("FU_INTEGRAL",fuInfo.getString("FU_INTEGRAL"));
		userForumProperties.put("FU_LOGIN_NUMS",fuInfo.get("FU_LOGIN_NUMS"));
		user.getExtendProperties().put(ExtendPropertyKeys.ForumPrivlege, userForumProperties);
	}

	@SuppressWarnings("rawtypes")
	public static String getFuId(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuId = userForumProperties.get("FU_ID").toString();
			result = fuId;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getFuCode(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuCode = userForumProperties.get("FU_CODE").toString();
			result = fuCode;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getFuName(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuName = userForumProperties.get("FU_NAME").toString();
			result = fuName;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getFuLevel(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuLevel = userForumProperties.get("FU_LEVEL").toString();
			result = fuLevel;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getFuState(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuState = userForumProperties.get("FU_STATE").toString();
			result = fuState;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getFuTel(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuTel = userForumProperties.get("FU_TEL").toString();
			result = fuTel;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getFuEmail(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuEmail = userForumProperties.get("FU_EMAIL").toString();
			result = fuEmail;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	private static String buildServiceId(Class serviceClass){
		String result = null;
		String className = serviceClass.getSimpleName();
		if (className.endsWith("Service")){
			result = StringUtil.lowerFirst(className);			
		}else{
			result = StringUtil.lowerFirst(className) + "Service";
		}
		return result;
	}
	
	private static <ServiceType> ServiceType lookupService(Class<ServiceType> serviceClass){
		String serviceId = buildServiceId(serviceClass);
		Object service = BeanFactory.instance().getBean(serviceId);
		return serviceClass.cast(service);
	}
	
	@SuppressWarnings({ "rawtypes" })
	public static String getFuIntegral(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuIntegral = userForumProperties.get("FU_INTEGRAL").toString();
			result = fuIntegral;
		}
		return result;
	}
	
	public static  void setFuLoginNums(User user,Integer loginNums,Integer fuIntegrals){
		UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = lookupService(UserLevel8BBSUserInfoManage.class);
		String fuCode = ForumPrivilegeInterceptor.getFuCode(user);
		userLevel8BBSUserInfoManage.updateUserLoginNums(fuCode,loginNums,fuIntegrals);
	}
	
	@SuppressWarnings("rawtypes")
	public static String getFuLoginNums(User user){
		String result = null;
		if(user!=null){
			HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
			String fuLoginNums = userForumProperties.get("FU_LOGIN_NUMS").toString();
			result = fuLoginNums;
		}
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static boolean isModerator(User user){
		boolean result = false;
		HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
		HashMap userIdentitys = (HashMap) userForumProperties.get("userIdentitys");
		for(int i=0;i<userIdentitys.size();i++){
			HashMap role = (HashMap) userIdentitys.get(String.valueOf(i));
			String roleId = role.get("ROLE_ID").toString();
			if(ForumConst.MODERATOR.equals(roleId)){
				result = true;
				break;
			}
		}
		return result;
	}	
	
	@SuppressWarnings("rawtypes")
	public static boolean isModerator(User user,String moduleId){
		boolean result = false;
		HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
		HashMap userIdentitys = (HashMap) userForumProperties.get("userIdentitys");
		String fuId = ForumPrivilegeInterceptor.getFuId(user);
		for(int i=0;i<userIdentitys.size();i++){
			HashMap role = (HashMap) userIdentitys.get(String.valueOf(i));
			String roleId = role.get("ROLE_ID").toString();
			if(ForumConst.MODERATOR.equals(roleId)){
				result = true;
				break;
			}
		}
		if(result){
			UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = lookupService(UserLevel8BBSUserInfoManage.class);
			DataRow row = userLevel8BBSUserInfoManage.retrieveForumUserSecRelRecord(fuId, moduleId);
			if(row != null && row.size()>0){
				result = true;
			}else{
				result = false;
			}
		}
		
		return result;
	}
	
	@SuppressWarnings("rawtypes")
	public static boolean isForumAdmin(User user){
		boolean result = false;
		HashMap userForumProperties = (HashMap) user.getExtendProperties().get("_ForumPrivlege_");
		HashMap userIdentitys = (HashMap) userForumProperties.get("userIdentitys");
		for(int i=0;i<userIdentitys.size();i++){
			HashMap role = (HashMap) userIdentitys.get(String.valueOf(i));
			String roleId = role.get("ROLE_ID").toString();
			if(ForumConst.ADMIN.equals(roleId)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public static boolean isGuest(User user){
		return user != null && "guest".equalsIgnoreCase(user.getUserCode());
	}
	
	public static Integer getUserLoginCount(String userCode){
		Integer result = 0;
		String key = userCode + DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL);
		synchronized (key) {
			if (!UserLoginCountCache.containsKey(key)){
				UserLoginCountCache.put(key, 0);
			}
			result = UserLoginCountCache.get(key);
		}
		return result;
	}
	
	private BigDecimal getBigDecimalCodeName(String typeId,String codeId) {
		BigDecimal codeName = new BigDecimal("0.00");
		UserLevel8BBSUserInfoManage userLevel8BBSUserInfoManage = lookupService(UserLevel8BBSUserInfoManage.class);
		DataRow row = userLevel8BBSUserInfoManage.getCodeName(typeId,codeId);
		codeName=new BigDecimal(row.getString("CODE_NAME"));
		return codeName;
	}
	
	
	public static synchronized void increaseUserLoginCount(User user){
		ForumPrivilegeInterceptor forumPrivilegeInterceptor = new ForumPrivilegeInterceptor();
		String userCode = ForumPrivilegeInterceptor.getFuCode(user);
		Integer currentCount = getUserLoginCount(userCode);
		Integer loginNum = 0;
		BigDecimal loginIntegral=forumPrivilegeInterceptor.getBigDecimalCodeName("INTEGRAL_CHANGE","DAILY_LOGIN");
		String fuLevel = ForumPrivilegeInterceptor.getFuLevel(user);
		if(ForumConst.REGION_COMMANDER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","REGION_COMMANDER"));
		}else if(ForumConst.SQUAD_LEADER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","SQUAD_LEADER"));
		}else if(ForumConst.PLATOON_LEADER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","PLATOON_LEADER"));
		}else if(ForumConst.COMPANY_COMMANDER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","COMPANY_COMMANDER"));
		}else if(ForumConst.BATTALION_COMMANDER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","BATTALION_COMMANDER"));
		}else if(ForumConst.REGIMENT_COMMANDER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","REGIMENT_COMMANDER"));
		}else if(ForumConst.BRIGADE_COMMANDER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","BRIGADE_COMMANDER"));
		}else if(ForumConst.DIVISION_COMMANDER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","DIVISION_COMMANDER"));
		}else if(ForumConst.CORPS_COMMANDER.equals(fuLevel)){
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","CORPS_COMMANDER"));
		}else{
			loginIntegral = loginIntegral.multiply(forumPrivilegeInterceptor.getBigDecimalCodeName("GRADE_INTEGRAL_CHANGE","SOLDIER"));
		}
		Integer loginIntegrals = Integer.valueOf(loginIntegral.setScale(0, BigDecimal.ROUND_HALF_UP).toString());
		if(currentCount<=2){
			loginNum = 1;
			String strHisLoginNums = getFuLoginNums(user);
			Integer hisLoginNums = Integer.valueOf(strHisLoginNums);
			Integer loginNums = loginNum+hisLoginNums;
			String strHisIntegral = getFuIntegral(user);
			Integer hisIntegral = Integer.valueOf(strHisIntegral);
			Integer fuIntegrals = hisIntegral+loginIntegrals;
			setFuLoginNums(user, loginNums,fuIntegrals);
		}
		String key = userCode + DateUtil.getDateByType(DateUtil.YYMMDD_HORIZONTAL);
		UserLoginCountCache.put(key, ++currentCount);
	}
}